package weaver.formmode.customjavacode.modeexpand.huangwm.SW003;

import com.customization.huangwm.utils.DBTools;
import com.customization.huangwm.utils.DateTools;
import com.customization.huangwm.utils.LogTool;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2022-10-19
 * @Version 1.0
 * @Description TODO 物料信息初始化
 **/
public class InitAssetInfo extends AbstractModeExpandJavaCodeNew {
    private LogTool logTool = new LogTool("/log/SW003/huangwm/model/action/InitAssetInfo", false);
    private DateTools dateTools = new DateTools();
    private DBTools dbTools = new DBTools();

    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        BaseBean bean = new BaseBean();
        try {
            User user = (User) param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            String importtype = param.getOrDefault("importtype", "0").toString();
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if (billid > 0 && modeid > 0) {
                    Map<String, String> mainDatas = new HashMap<>();
                    String sql1 = "select * from uf_003_AssteInfo where id=" + billid;
                    RecordSet rs1 = new RecordSet();
                    rs1.execute(sql1);
                    rs1.next();
                    String[] columns = rs1.getColumnName();
                    for (int i = 0; i < columns.length; i++) {
                        mainDatas.put(columns[i].toLowerCase(), rs1.getString(columns[i]));
                    }
                    String yyType = dbTools.getEapTypeIdCompareYYID(rs1,mainDatas.get("assettype2"));
                    //導入的物料初始數量為0
                    rs1.executeUpdate("update uf_003_AssteInfo set totalQuantity=0,inApplicationQuantity=0,InventoryQuantity=0,usedQuantity=0,assetStatus=0 where id="+billid);
                    //關聯用友物料類型
                    rs1.executeUpdate("update uf_003_AssteInfo set assetType="+yyType+"  where id="+billid);
                    logTool.writeLog("update uf_003_AssteInfo set totalQuantity=0,inApplicationQuantity=0,InventoryQuantity=0,usedQuantity=0,assetStatus=0 where id="+billid);
                    logTool.writeLog("update uf_003_AssteInfo set assetType="+yyType+"  where id="+billid);
                } else {
                    result.put("errmsg", "保存動作擴展接口执行失败 billid为空");
                    result.put("flag", "false");
                    return result;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logTool.writeLog("保存動作擴展接口異常" + e.getMessage());
            result.put("errmsg", "保存動作擴展接口異常 msg=" + e.getMessage());
            result.put("flag", "false");
        }
        return result;
    }
}
